﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Graph2
{
	public partial class FunctionForm : Form
	{
		private Function m_Function;

		public Function Function
		{
			get
			{
				return m_Function;
			}
		}

		public FunctionForm()
		{
			InitializeComponent();
		}

		private void CancelBtn_Click(object sender, EventArgs e)
		{
			this.DialogResult = DialogResult.Cancel;
			Close();
		}

		private void ProcessForm()
		{
			try
			{
				m_Function = new StandardFunction(StdFuncExpressionTxt.Text);
			}
			catch (Function.ParseException err)
			{
				MessageBox.Show(this, "Parse error at " + err.Position + ": " + err.Message, "Parsing Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
				return;
			}
			DialogResult = DialogResult.OK;
			Close();
		}

		private void OKBtn_Click(object sender, EventArgs e)
		{
			ProcessForm();
		}

		private void StdFuncExpressionTxt_KeyPress(object sender, KeyPressEventArgs e)
		{
			if (e.KeyChar == '\r')
			{
				ProcessForm();
				e.Handled = true;
			}
		}

		private void FunctionForm_Load(object sender, EventArgs e)
		{
			FunctionTypeCombo.SelectedIndex = 0;
			StdFuncExpressionTxt.Focus();
		}
	}
}
